'use strict';
const db = uniCloud.database();
const _ = db.command;

exports.main = async (event, context) => {
    const { username, password, email } = event;
    // 检查用户名是否已存在
    const checkRes = await db.collection('users').where({
        username: username
    }).get();
    if (checkRes.data.length > 0) {
        return {
            code: 400,
            msg: '用户名已存在'
        };
    }
    // 插入新用户信息，确保密码是数字类型
    const insertRes = await db.collection('users').add({
        username: username,
        password: Number(password), 
        email: email
    });
    if (insertRes.id) {
        return {
            code: 200,
            msg: '注册成功'
        };
    } else {
        return {
            code: 500,
            msg: '注册失败，请稍后重试'
        };
    }
};